<template>
  <div class="login-group">
    <div class="login-info">
      <h3>用户登录</h3>
      <el-form :rules="rules" ref="loginFrom" :model="login" label-position="right" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="login.username" placeholder="输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密   码" prop="password">
          <el-input v-model="login.password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loginFun">登录</el-button>

        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import commonRules from '@/util/VaildatorRules'
import axios from "axios";

export default {
  name: 'Login',
  data() {
    return {
      login: {
        username: 'admin',
        password: 'admin'
      },
      rules: {
        username: [
          {required: true, message: '请输入登录名称', trigger: 'blur'},
          {min: 2, max: 5, message: '长度在2到5之间', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入登录密码', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
        ]
        // ...commonRules,
      }
    }
  },
  methods: {
    loginFun() {
      this.$refs.loginFrom.validate(valid => {
        if (valid) {
          // if (this.login.username==='admin'&&this.login.password==='admin'){
          //   this.$alert("登录成功")
          // }else{
          //   this.$message({
          //     type:'error',
          //     message:'用户名或密码不正确'
          //   })
          // }

          this.$axios.post('http://192.168.162.100:3000/login/doLogin', this.login).then(({data}) => {
            if (data.code === 1) {
              this.$message({
                type: 'success',
                message: '登录成功',
                onClose: () => {
                  console.log('进入系统首页')
                  // this.$router.push('home')
                  this.$router.replace('adout')
                }
              })
            }else {
              this.$alert(data.msg,{type:'error'})
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.login-group {
  height: 100%;
  width: 100%;
  background: url("../assets/images/login-bg.png") no-repeat center/cover;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;

  .login-info {
    width: 30%;
    background-color: rgba(0, 0, 0, .3);
    padding: 15px;

    h3 {
      text-align: center;
    }
  }
}
</style>
